<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :defaults.sync="defaults"
      :page.sync="page"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @row-update="handleUpdateData"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <!-- <el-button type="warning" size="small" plain icon="el-icon-download" @click="handleExport">导出 </el-button> -->
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val)"></user-table-config>
      </template>
    </avue-crud>
    <!-- <export-excel
      :visible="exprotVisible"
      :option="option"
      :page="page"
      :data="exportData"
      name="锡膏管理导出"
      :loading="btnLoading"
      @selectData="handleSelectData"
      @close="handleClose"
    ></export-excel> -->
  </basic-container>
</template>

<script>
import { getList } from '@/api/equipment-manage/solder-code-manage.js';
import { getList as getSyncList } from '@/api/warehouse/material';
import { mapGetters } from 'vuex';
import exportExcel from '@/components/export-excel';
import { exprot } from '@/api/repair/failureconfig';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
export default {
  components: {
    exportExcel,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      exprotVisible: false,
      exportData: [],
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 440px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        border: true,
        index: true,
        indexLabel: '序号',
        addBtn: false,
        delBtn: false,
        menu: false,
        selection: true,
        searchMenuPosition: 'right',
        searchMenuSpan: 4,
        searchSpan: 5,
        menuWidth: 200, //弹框内字段宽度属性
        //   dialogWidth: 880, //弹框宽度属性
        labelWidth: '180',
        //   searchMenuSpan: 7,
        dialogClickModal: false,
        columnBtn: false,
        column: [
          {
            label: '锡膏条码',
            prop: 'barCode',
            search: true
          },
          {
            label: '锡膏编码',
            prop: 'code',
            search: true
          },
          {
            label: '锡膏批次',
            prop: 'lotNo',
            search: true
          },
          {
            label: '生产日期',
            prop: 'productionDate'
          },
          {
            label: '失效日期',
            prop: 'expirationExpire'
          },
          {
            label: '当前状态',
            prop: 'status',
            search: true,
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=part_indirect_status',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '更新时间',
            prop: 'updateTime'
          },
          {
            label: '使用次数',
            prop: 'timesUsed'
          },
          {
            label: '最长使用时间(h)',
            prop: 'maxUsageTime'
          },
          {
            label: '最短回温时间(h)',
            prop: 'timeMinRise'
          },
          {
            label: '最短搅拌时间(m)',
            prop: 'timeMinStir'
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(['permission']),
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    handleImport() {
      this.excelBox = true;
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      params.type = 'PACK';
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = this._map(data.records, (item, index) => {
          item.status = String(item.status);
          return {
            ...item,
            expirationExpire: this.$moment(item.productionDate).add(item.expirationDays, 'days').format('YYYY-MM-DD HH:mm:ss')
          };
        });
        this.exportData = this.data;
        // this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },

    handleExport() {
      this.exportData = this.data;
      this.exprotVisible = true;
    },
    handleClose(val) {
      this.exprotVisible = false;
      this.data = this.exportData.splice(0, this.page.pageSize);
    },
    handleSelectData(val) {
      let current = 1;
      let pageSize = this.page.pageSize;
      if (val === 'all') {
        // 接口返回上限500条数据
        pageSize = 500;
      } else {
        current = this.page.currentPage;
      }
      this.btnLoading = true;
      getList(current, pageSize, Object.assign({ type: 'PACK', ...this.query })).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = this._map(data.records, (item, index) => {
          return {
            ...item,
            expirationExpire: this.$moment(item.productionDate).add(item.expirationDays, 'days').format('YYYY-MM-DD HH:mm:ss')
          };
        });
        this.exportData = this.data;
        this.btnLoading = false;
      });
    }
  }
};
</script>
